﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Win10_1607版本号计算器
{
    public partial class Coder : Form
    {
        public Coder()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string baseType = (string)comboBox1.SelectedItem;//带转化的数值类型
                string goalType = (string)comboBox2.SelectedItem;//目标类型
                string basenum = this.textBox1.Text;
                switch (baseType)
                {
                    case "二进制":
                        {
                            switch (goalType)
                            {
                                case "二进制":
                                    this.textBox2.Text = basenum;
                                    break;
                                case "八进制":
                                    this.textBox2.Text = Convert.ToString(Convert.ToInt32(basenum, 2), 8);
                                    break;
                                case "十进制":
                                    this.textBox2.Text = Convert.ToInt32(basenum, 2).ToString();
                                    break;
                                case "十六进制":
                                    this.textBox2.Text = Convert.ToString(Convert.ToInt32(basenum, 2), 16);
                                    break;
                            }
                            break;
                        }
                    case "八进制":
                        {
                            switch (goalType)
                            {
                                case "二进制":
                                    this.textBox2.Text = Convert.ToString(Convert.ToInt32(basenum, 8), 2);
                                    break;
                                case "八进制":
                                    this.textBox2.Text = basenum;
                                    break;
                                case "十进制":
                                    this.textBox2.Text = Convert.ToInt32(basenum, 8).ToString();
                                    break;
                                case "十六进制":
                                    this.textBox2.Text = Convert.ToString(Convert.ToInt32(basenum, 8), 16);
                                    break;
                            }
                            break;
                        }
                    case "十进制":
                        {
                            switch (goalType)
                            {
                                case "二进制":
                                    this.textBox2.Text = Convert.ToString(Convert.ToInt32(basenum), 2);
                                    break;
                                case "八进制":
                                    this.textBox2.Text = Convert.ToString(Convert.ToInt32(basenum), 8);
                                    break;
                                case "十进制":
                                    this.textBox2.Text = basenum;
                                    break;
                                case "十六进制":
                                    this.textBox2.Text = Convert.ToString(Convert.ToInt32(basenum), 16);
                                    break;

                            }
                            break;
                        }
                    case "十六进制":
                        {
                            switch (goalType)
                            {
                                case "二进制":
                                    this.textBox2.Text = Convert.ToString(Convert.ToInt32(basenum, 16), 2);
                                    break;
                                case "八进制":
                                    this.textBox2.Text = Convert.ToString(Convert.ToInt32(basenum, 16), 8);
                                    break;
                                case "十进制":
                                    this.textBox2.Text = Convert.ToInt32(basenum, 16).ToString();
                                    break;
                                case "十六进制":
                                    this.textBox2.Text = basenum;
                                    break;
                            }
                            break;
                        }
                }
            }
            catch(Exception ex)//扑捉异常，处理存在不太妥当。直接将异常归结为用户操作错误，提示重新操作
            {
                MessageBox.Show("输入数字格式可能不正确，请确认输入数字格式");
            }
            
        }

        //将进制字符串转化为数字
        public int Typechage(string str)
        {
            switch (str)
            {
                case "十进制":
                    return 10;
                case "八进制":
                    return 8;
                case "二进制":
                    return 2;
                case "十六进制":
                    return 16;
                default:
                    return 2;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://baike.baidu.com/item/%E8%BF%9B%E5%88%B6/317457?fr=aladdin");
        }
    } 
}
